package com.handinfo.android.ui.window;

import java.io.DataInputStream;

import android.graphics.Bitmap;
import android.widget.Toast;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.Role;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTabPage;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

/**
 * 
 * @author Xiaoyingying
 * 
 * 婚姻
 * 
 */

public class UIMarriage implements IUIWindows
{
	public static final byte NPCWINDOW = 1; //npc窗口
	public static final byte PAGEWINDOW = 2; //社交界面窗口

//	private long m_marrige_fee;
	private String m_wifeName;
	private String m_husbandName;
	private Role role;

	private Bitmap img_button;
	private Bitmap img_anxia;
	private Bitmap img_yiyuan;
	private Bitmap img_yiyuanAX;
	private Bitmap img_taoxin;
	private Bitmap img_fuqi;
	private String m_spouseName;
	public byte m_state = 0;

	public void init()
	{
//		img_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		img_button = UIWindows.createImage("/img/newui/anniu_6.gnp");
		img_anxia = UIWindows.createImage("/img/newui/anniu_6ax.gnp");
//		img_title = UIWindows.createImage("/img/newui/yinyuan_1.gnp");
		img_yiyuan = UIWindows.createImage("/img/newui/qianliyinyuan_1.gnp");
		img_yiyuanAX = UIWindows.createImage("/img/newui/qianliyinyuan_2.gnp");
		img_taoxin = UIWindows.createImage("/img/newui/fuqi_1.gnp");
		img_fuqi = UIWindows.createImage("/img/newui/fuqi_2.gnp");
	}

	public void open(byte index)
	{
		m_state = index;

		switch (index) {
			case NPCWINDOW: //求婚离婚界面
				if (input_Frame == null || (!DWControlsManager.getInstance().contains(input_Frame)))
					inputName();
				break;
			case PAGEWINDOW: //社交界面中的婚姻一页
				dissmiss_time = 0;
				pageWindow();
				break;
		}
	}

	public void close(byte index)
	{
		m_state = 0;
		dissmiss_time = 0;
	}

	public void dispose()
	{
	}

	private long dissmiss_time = 0;

	public void logic()
	{
		if (role != null && role.m_marriage_CD > 0 && m_hunyin_mainPage != null)
		{
			long nowTime = System.currentTimeMillis() / 1000;
			long intervalTime = nowTime - role.m_marriage_CDStar;
			if (intervalTime - dissmiss_time > 0)
			{
				Tools.debugPrintln("倒计时……");
				dissmiss_time = intervalTime;
				setCDLable(role.m_marriage_CD - dissmiss_time);
			}
		}
	}

//	private DWButton m_getMarried; //结婚按钮
//	private DWButton m_breakMarried; //离婚按钮
//	private DWFrame m_npcMarriage;
//	private DWTitle m_npcTitle;
//	private DWImageBox m_close;
//
//	private void npcMarriage()
//	{
//		m_npcMarriage = new DWFrame(DWFrame.FRAME_TYPE_HALF_HCENTER);
//		m_npcMarriage.setClickClose(false);
//
//		m_npcTitle = new DWTitle("姻缘", m_npcMarriage);
//		m_npcTitle.setBackground(img_title, false);
//		m_npcMarriage.addControl(m_npcTitle);
//
//		m_close = new DWImageBox(img_close);
//		m_close.setNearAnchor(m_npcTitle, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
//		m_close.setTouchZoomIn(20, 20);
//		m_close.addListener(new DWListener() {
//			public void OnDoubleClick()
//			{
//			}
//
//			public void OnClick()
//			{
//				close((byte) 0);
//			}
//		});
//		m_npcMarriage.addControl(m_close);
//
//		DWBackground m_background = new DWBackground(m_npcMarriage.getShowWidth() - 40,
//				m_npcMarriage.getShowHeight() - 40);
//		m_background.setNearAnchor(m_npcMarriage, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
//		m_npcMarriage.addControl(m_background);
//
//		m_getMarried = new DWButton("我要结婚", img_button);
//		m_getMarried.setNearAnchor(m_background, Tools.HCENTER_BOTTOM, Tools.HCENTER_VCENTER, 0, -60);
//		m_getMarried.setDownImage(img_anxia);
//		m_getMarried.addListener(new DWListener() {
//			public void OnDoubleClick()
//			{
//			}
//
//			public void OnClick()
//			{
//				inputName();
//			}
//		});
//		m_npcMarriage.addControl(m_getMarried);
//
//		m_breakMarried = new DWButton("我要离婚", img_button);
//		m_breakMarried.setNearAnchor(m_background, Tools.HCENTER_BOTTOM, Tools.HCENTER_VCENTER, 0, 60);
//		m_breakMarried.setDownImage(img_anxia);
//		m_breakMarried.addListener(new DWListener() {		
//			public void OnDoubleClick()
//			{
//			}
//			
//			public void OnClick()
//			{
//				DWGameManager.getInstance().getSendMessage().sendDivorceApplication();
//			}
//		});
//		m_npcMarriage.addControl(m_breakMarried);
//	}

	/**
	 * 输入配偶名字界面
	 */
	public DWInputBox input_inputName;
	public DWFrame input_Frame;

	private void inputName()
	{
		input_Frame = new DWFrame(450, 350);
		input_Frame.setClickClose(false);

		DWTextbox creat_family_label = new DWTextbox("请输入求婚对象的名字  ");
		creat_family_label.setFont(DWFont.getFont(30));
		creat_family_label.setNearAnchor(input_Frame, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, 50);
		input_Frame.addControl(creat_family_label);

		input_inputName = new DWInputBox(200, 40, input_Frame);
		input_inputName.setNearAnchor(creat_family_label, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, -(10 + img_button
				.getWidth()) / 2, 20);
		input_Frame.addControl(input_inputName);

		DWButton addName = new DWButton("好友", img_button);
		addName.setDownImage(img_anxia);
		addName.setNearAnchor(input_inputName, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 10, 0);
		addName.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				UIWindows.getInstance().m_social.openUISocialFriendToHunYin();
			}
		});
		input_Frame.addControl(addName);

		DWTextbox m_tips_lable = new DWTextbox("求婚需要花费" + m_proposeMoney + "元宝");
		m_tips_lable.setFont(DWFont.getFont(30));
		m_tips_lable.setNearAnchor(creat_family_label, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 100);
		input_Frame.addControl(m_tips_lable);

		DWButton creat_family_button1 = new DWButton("确定", img_button);
		creat_family_button1.setDownImage(img_anxia);
		creat_family_button1.setNearAnchor(input_Frame, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 5, -5);
		creat_family_button1.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				String msg = input_inputName.getText();
				if (msg == null || msg.equals(""))
				{
					DWGameManager.getInstance().showToast("配偶名称不能为空", Toast.LENGTH_SHORT);
				}
				else if (msg.length() < 2)
				{
					DWGameManager.getInstance().showToast("输入过短!", Toast.LENGTH_SHORT);
				}
				else
				{
					DWGameManager.getInstance().getSendMessage().sendProposeApplication(msg);
					m_spouseName = msg;
					DWControlsManager.getInstance().removeControl(input_Frame);
				}
			}
		});
		DWButton creat_family_button2 = new DWButton("取消", img_button);
		creat_family_button2.setDownImage(img_anxia);
		creat_family_button2.setNearAnchor(input_Frame, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -5, -5);
		creat_family_button2.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(input_Frame);
				UISocial social = UIWindows.getInstance().m_social;
				if (social.m_si_fram != null && DWControlsManager.getInstance().contains(social.m_si_fram)) {
					UIWindows.getInstance().m_social.close((byte) 0);
				}
			}
		});

		input_Frame.addControl(creat_family_button1);
		input_Frame.addControl(creat_family_button2);

		DWControlsManager.getInstance().addControl(input_Frame);
	}

	public DWTabPage m_hunyin_mainPage;//婚姻頁
	private DWImageBox m_hunyin_taoxin;
	private DWImageBox m_hunyin_fuqi;
	private DWTextbox m_hunyin_name[] = new DWTextbox[2];
	private DWTextbox m_hunyin_msgTexbox;
	private DWButton m_hunyin_button;
	private DWLabel m_hunyin_CDLable;
	private DWTextbox m_hunyin_tipsTextbox;

	private String msg = "<mark c=FFBB11>婚姻规则：</mark>`达到40级后可以和最爱的另一半，去凌霄殿月下老人处举行婚礼。相亲相爱的夫妻能够获得很多福利：充值配偶获赠元宝、组队获取金币增多、可以传送到对方身边。组队杀敌人或怪物，婚姻任务和特定道具可以提升恩爱值。愿你早日摆脱孤单，找到生命中的最爱，有情人终成眷属。";

	public void pageWindow()
	{
		role = DWGameManager.getInstance().m_role;
		dissmiss_time = 0;
		m_hunyin_mainPage = null;

//		if(role.m_spouseName==null||role.m_spouseName.equals("")|| UIWindows.getInstance().m_social.m_si_tc==null)
//			return null;
		if (UIWindows.getInstance().m_social.m_si_tc == null)
			return;

		if (role.m_sex == 1)
		{
			m_wifeName = role.m_spouseName;
			m_husbandName = role.m_name;
		}
		else
		{
			m_husbandName = role.m_spouseName;
			m_wifeName = role.m_name;
		}

		m_hunyin_mainPage = new DWTabPage();
		m_hunyin_mainPage.setName("婚姻");
		UIWindows.getInstance().m_social.m_si_tc.addTabPage(m_hunyin_mainPage);

		refreshPage();
	}

	/**
	 * 刷新
	 */
	public void refreshPage()
	{
		if (m_hunyin_mainPage == null)
			return;

		m_hunyin_mainPage.removeAllControl();

		m_hunyin_taoxin = new DWImageBox(img_taoxin);
		m_hunyin_taoxin.setNearAnchor(m_hunyin_mainPage, Tools.HCENTER_TOP, Tools.LEFT_TOP, (m_hunyin_mainPage
				.getShowWidth() - 10) >> 2, 10);
		m_hunyin_mainPage.addControls(m_hunyin_taoxin);

		m_hunyin_fuqi = new DWImageBox(img_fuqi);
		m_hunyin_fuqi.setNearAnchor(m_hunyin_mainPage, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 10, -20);
		m_hunyin_mainPage.addControls(m_hunyin_fuqi);

		if (role.m_spouseName != null && (!role.m_spouseName.equals("")))
		{
			m_hunyin_name[0] = new DWTextbox(m_wifeName);
			m_hunyin_name[0].setFont(DWFont.getFont(25));
			m_hunyin_name[0].setNearAnchor(m_hunyin_taoxin, Tools.RIGHT_BOTTOM, Tools.HCENTER_BOTTOM, -15, -35);
			m_hunyin_mainPage.addControls(m_hunyin_name[0]);

			m_hunyin_name[1] = new DWTextbox(m_husbandName);
			m_hunyin_name[1].setFont(DWFont.getFont(25));
			m_hunyin_name[1].setNearAnchor(m_hunyin_taoxin, Tools.LEFT_BOTTOM, Tools.HCENTER_BOTTOM, 15, -35);
			m_hunyin_mainPage.addControls(m_hunyin_name[1]);
		}

		setMessage();

		m_hunyin_button = new DWButton("", img_yiyuan);
		m_hunyin_button.setDownImage(img_yiyuanAX);
		m_hunyin_button.setNearAnchor(m_hunyin_mainPage, Tools.LEFT_BOTTOM, Tools.HCENTER_BOTTOM, 15, -20);
		m_hunyin_button.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWGameManager.getInstance().getSendMessage().sendDivorceTransfer();
			}
		});

		if (!role.m_marriage_transfer) //没有传送技能
			m_hunyin_button.setFailureTime(Integer.MAX_VALUE);
		if (role.m_marriage_CD > 0) //冷却时间中
			m_hunyin_button.setFailureTime(Integer.MAX_VALUE);
		m_hunyin_mainPage.addControls(m_hunyin_button);

		m_hunyin_tipsTextbox = new DWTextbox(msg, (m_hunyin_mainPage.getShowWidth() - 10) / 2 - 20);
//		m_hunyin_tipsTextbox.setFont(DWFont.getFont(18));
		m_hunyin_tipsTextbox.setNearAnchor(m_hunyin_mainPage, Tools.HCENTER_TOP, Tools.HCENTER_TOP, (m_hunyin_mainPage
				.getShowWidth() - 10) / 4,
				(m_hunyin_mainPage.getShowHeight() - m_hunyin_button.getShowHeight() -m_hunyin_tipsTextbox.getShowHeight()- 20) / 2);
		m_hunyin_mainPage.addControls(m_hunyin_tipsTextbox);
	}

	/**
	 * 設置婚姻界面信息
	 */
	private void setMessage()
	{
		if (m_hunyin_msgTexbox != null)
		{
			m_hunyin_mainPage.removeControl(m_hunyin_msgTexbox);
			m_hunyin_msgTexbox = null;
		}
		String msg = "婚姻等级：" + role.m_marriage_lv + "`恩爱值：" + role.m_marriage_exp + "/" + role.m_marriage_maxExp
				+ "`充值元宝赠送：" + role.m_marriage_yuanbao + "%`掉落金币：" + role.m_marriage_jinbi + "%";
		m_hunyin_msgTexbox = new DWTextbox(msg);

		m_hunyin_msgTexbox.setNearAnchor(m_hunyin_fuqi, Tools.LEFT_BOTTOM, Tools.RIGHT_BOTTOM, -20, 0);
		m_hunyin_mainPage.addControls(m_hunyin_msgTexbox);
	}

	/**
	 * 设置婚姻传送冷却时间
	 */
	private void setCDLable(long CDTime)
	{
		if (m_hunyin_CDLable != null)
		{
			m_hunyin_mainPage.removeControl(m_hunyin_CDLable);
			m_hunyin_CDLable = null;
		}

		if (CDTime > 0)
		{
			m_hunyin_CDLable = new DWLabel("冷却：" + UIBag.getTime(CDTime, UIBag.MINUTETYPE));
			m_hunyin_CDLable.setNearAnchor(m_hunyin_button, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 20, 0);
			m_hunyin_CDLable.setBackgroundColor(Tools.ALPHA);
			m_hunyin_mainPage.addControls(m_hunyin_CDLable);
//			m_hunyin_button.setFailureTime(Integer.MAX_VALUE);
		}
		else
		{
			role.m_marriage_CD = 0;
			dissmiss_time = 0;
			m_hunyin_button.setFailureTime(0);
		}
	}

	/**
	 * 打开结婚界面
	 */
	private long m_proposeMoney;

	public void recvMarriageOpenUI(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();
			if (state == 1)
			{
				m_proposeMoney = dis.readLong();
				open(NPCWINDOW);
			}
			else
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "不能打开婚姻界面");
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 求婚响应
	 */
	public void recvMarriageConfirm(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();
			if (state == 1)
			{
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "操作成功");
			}
			else
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "求婚申请已发出");
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 被求婚
	 */
	public void recvMarriageRequest(DataInputStream dis)
	{
		try {
			final long ID = dis.readLong();
			final String name = dis.readUTF();
			final DWMessageBox msg = new DWMessageBox("提示", name + "深爱着你，愿意与您结尾终身的伴侣，生死相随！您是否同意？！", "同意", "拒绝");
			msg.addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					byte state = 0;
					if (msg.getResult() == DWMessageBox.SOFTKEY_OK)
					{
						state = 1;
//						DWGameManager.getInstance().m_role.m_spouseID=ID;
//						DWGameManager.getInstance().m_role.m_spouseName=name;
					}

					DWGameManager.getInstance().getSendMessage().sendProposeConfirm(state, ID);
					DWControlsManager.getInstance().removeControl(msg);
				}
			});
			DWControlsManager.getInstance().addControl(msg);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 求婚结果 拒绝或接受
	 */
	public void recvMarriageResult(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();
			if (state == 1)
			{
//				role.m_spouseName=m_spouseName;
			}

			String msg = dis.readUTF();
			DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, msg);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 离婚申请响应
	 */
	public void recvDivorceApplication(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();
			if (state == 0)
			{
				String msg = dis.readUTF();
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, msg);
			}
			else if (state == 1)
			{
				String itemName = dis.readUTF();
				short itemIcon = dis.readShort();
				final DWMessageBox msgbox = new DWMessageBox("提示", "一日夫妻百日恩，确定解除夫妻关系？！`离婚需要花费" + itemName + "道具");
				msgbox.addListener(new DWListener() {

					public void OnDoubleClick()
					{
					}

					public void OnClick()
					{
						DWControlsManager.getInstance().removeControl(msgbox);
						byte state = 0;
						if (msgbox.getResult() == DWMessageBox.SOFTKEY_OK)
							state = 1;

						DWGameManager.getInstance().getSendMessage().sendDivorceConfirm(state);
					}
				});

				DWControlsManager.getInstance().addControl(msgbox);
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 离婚响应
	 */
	public void recvDivorceResult(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();
			if (state == 0)
			{
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "离婚失败");
			}
			else if (state == 1)
			{
				role.m_spouseID = 0;
				role.m_spouseName = "";

				String msg = dis.readUTF();
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, msg);
			}

		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
}
